package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.OperLogDao;
import com.khyr.entity.OperLog;
import com.khyr.service.OperLogService;
import com.khyr.vo.OperLogVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class OperLogServiceImpl implements OperLogService {
    @Autowired
    private OperLogDao operLogDao;

    @Override
    public R<Page<OperLog>> selectOperLogByCond(OperLogVo operLogVo) {
        //分页
        Page<OperLog> page = new Page<>(operLogVo.getPageNum(), operLogVo.getPageSize());
        //模糊
        QueryWrapper<OperLog> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(operLogVo.getTitle())){
            queryWrapper.like("title", operLogVo.getTitle());
        }
        if (StringUtils.hasText(operLogVo.getOperName())){
            queryWrapper.like("oper_name", operLogVo.getOperName());
        }
        if (StringUtils.hasText(operLogVo.getStatus())){
            queryWrapper.like("status", operLogVo.getStatus());
        }
        if (StringUtils.hasText(operLogVo.getBusinessType())){
            queryWrapper.like("business_type", operLogVo.getBusinessType());
        }
        if (StringUtils.hasText(operLogVo.getBeginTime())&& StringUtils.hasText(operLogVo.getEndTime())){
            queryWrapper.between("create_time", operLogVo.getBeginTime(), operLogVo.getEndTime());
        }
        Page<OperLog> PageInfo = operLogDao.selectPage(page, queryWrapper);
        if (PageInfo != null){
            return new R<>(200,"查询成功", PageInfo);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<Integer> deleteOperLogByOperLogIds(List<Long> ids) {
        if (ids != null){
            int delete = operLogDao.deleteOperLogByOperLogIds1(ids);
            if (delete > 0){
                return new R<>(200,"删除成功", delete);
            }
        }
        return new R<>(500,"删除失败", null);
    }

    @Override
    public R<Integer> clearOperLog() {
        if (operLogDao.clearOperLog(null) > 0){
            return new R<>(200,"清空成功", null);
        }
        return new R<>(500,"清空失败", null);
    }
}
